package com.dreamwhale.machinery.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dreamwhale.machinery.config.DataResult;
import com.dreamwhale.machinery.dao.CarTypesMapper;
import com.dreamwhale.machinery.entity.AboutUs;
import com.dreamwhale.machinery.entity.CarManage;
import com.dreamwhale.machinery.entity.CarTypes;
import com.dreamwhale.machinery.service.AboutUsService;
import com.dreamwhale.machinery.service.CarManageService;
import com.dreamwhale.machinery.user.entity.UserShop;
import com.dreamwhale.machinery.utils.RedisUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 车辆入驻 前端控制器
 * </p>
 *
 * @author Author
 * @since 2020-08-04
 */
@RestController
@RequestMapping("/user")
public class CarManageController {

    @Resource
    private CarManageService carManageService;

    @Resource
    private RedisUtils<String> strRedisUtils;

    @Resource
    private CarTypesMapper carTypesMapper;

    @Resource
    private AboutUsService aboutUsService;

    /**
     * 车辆入驻
     *
     * @param carManage
     * @return
     */
    @RequestMapping("/insertCarManages")
    public DataResult insertCarManages(CarManage carManage, String code) {
        String read = strRedisUtils.read(String.format("Sms-%s", carManage.getPhone()));
        if (read.equals(code)) {
            int count = carManageService.insertCarManages(carManage);
            if (count > 0) {
                return DataResult.ok(1);
            }
            return DataResult.build(300, "入驻失败");
        } else {
            return new DataResult(300, "验证码不正确", null);
        }
    }


    /**
     * 查询是否入驻
     *
     * @return
     */
    @RequestMapping("/selectCarManageCount")
    public DataResult selectCarManageCount(String userId) {
        int count = carManageService.selectCarManageCount(userId);
        if (count > 0) {
            return DataResult.build(300, "请勿重复入驻");
        } else {
            return DataResult.ok(1);
        }
    }

    /**
     * 查询车辆类型
     *
     * @return
     */
    @RequestMapping("/selectCarTypes")
    public DataResult selectCarTypes() {
        QueryWrapper<CarTypes> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag", 0);
        return DataResult.ok(carTypesMapper.selectList(queryWrapper));
    }
}

